<template>
    <div>
        <h2>添加医生信息表</h2>
        <table>
            <tbody>
                <tr>
                    <td>医生姓名</td>
                    <td>
                        <input type="text" v-model="pdate.userName">
                    </td>
                </tr>
                <tr>
                    <td>医生照片</td>
                    <td>
                        <input type="file" @change="photo">
                        <img :src="pdate.img" width="60px" >
                    </td>
                </tr>
                <tr>
                    <td>医生科室</td>
                    <td>
                        <select v-model="pdate.ttid">
                            <option value=-1>请选择</option>
                            <option v-for="item in pbind" :key="item.tid" :value="item.tid">{{ item.tname }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>入职时间</td>
                    <td>
                        <input type="date" v-model="pdate.createDate">
                    </td>
                </tr>
                <tr>
                    <td> </td>
                    <td>
                        <input type="button" value="添加" @click="addUser">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">

import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

let pdate = reactive({
    userName: "",
    img: "",
    ttid: -1,
    createDate: null
})

let pbind = ref([]);

onMounted(() => {
    Binds();
})


const Binds = () => {
    axios({
        url: "/api/Hotel/Binds",
        method: "get"
    }).then((res) => {
        pbind.value = res.data;

    })
}



const photo = (e: any) => {
    console.log(e);
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);

    axios({
        url: '/api/Hotel/Photo',
        method: 'post',
        data: f
    }).then((res) => {
        if (res.data == "文件格式不正确") {
            alert("文件格式不正确");
            return;
        } else if (res.data == "文件超出2M") {
            alert("文件超出2M");
            return;
        }
        pdate.img = res.data;
    }).catch((err) => {
            console.log(err);
            return;
        })
}





const addUser = () => {

    if (pdate.userName == "") {
        alert("请输入医生姓名");
        return;
    }
    if (pdate.img == "") {
        alert("请输入医生照片");
        return;
    }
    if (pdate.ttid == -1) {
        alert("请选择医生科室");
        return;
    }
    if (pdate.createDate == null) {
        alert("请选择入职时间");
        return;
    }




    axios({
        url: "/api/Hotel/AddUser",
        method: "post",
        data: pdate
    }).then((res) => {
        if (res.data > 0) {
            alert("添加成功");
            router.push("/ShowUserView");
        } else {
            alert("添加失败");
            return;
        }
    })
}


</script>

<style scoped></style>